﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernateSample.Domain.Entities;
using NHibernate;
using NHibernate.Criterion;

namespace NHibernateSample.Data
{
    public class QueryCriteriaAPI
    {
        protected ISession Session { get; set; }
        public QueryCriteriaAPI(ISession session)
        {
            Session = session;
        }

        public QueryCriteriaAPI()
        {
            Session = new NHibernateHelper().GetSession();
        }

        public IList<Customer> GetCustomers()
        {
            ICriteria crit = Session.CreateCriteria(typeof(Customer));
            crit.SetMaxResults(3);
            crit.Add(Restrictions.IsNotNull("LastName"));
            crit.AddOrder(NHibernate.Criterion.Order.Desc("Id"));
            crit.AddOrder(new NHibernate.Criterion.Order("FirstName",true));
            IList<Customer> customers = crit.List<Customer>();
            return customers;
        }

        public Customer Get(int customerID)
        {
            return Session.Get<Customer>(customerID);
        }

    }
}
